package test3;

import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * @author: mornd
 * @dateTime: 2023/5/11 - 15:25
 * 其它自动配置，mybatis，datasource，transaction, mvc
 */
public class App {

    public static void main(String[] args) throws Exception {
        GenericApplicationContext context = new GenericApplicationContext();
        StandardEnvironment env = new StandardEnvironment();

        // 设置让容器可以读取 application.properties 下的属性
        env.getPropertySources().addFirst(
                new ResourcePropertySource(new ClassPathResource("application.properties")));

        context.setEnvironment(env);
//        System.out.println(env.getProperty("spring.datasource.driver-class-name"));

        AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());
        context.registerBean(Config.class);

        String packageName = App.class.getPackage().getName();
        AutoConfigurationPackages.register(context.getDefaultListableBeanFactory(), packageName);

        context.refresh();

        Arrays.stream(context.getBeanDefinitionNames()).forEach(name -> {
            String desc = context.getBeanDefinition(name).getResourceDescription();
            if(desc != null) {
                System.out.println("name：" + name + "  来源：" + desc);
            }
        });

        context.close();
    }


    @Import(MyImportSelector.class)
    //@EnableAutoConfiguration
    static class Config {

    }

    static class MyImportSelector implements DeferredImportSelector {

        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            return new String[] {
                    DataSourceAutoConfiguration.class.getName(),
                    MybatisAutoConfiguration.class.getName(),
                    DataSourceTransactionManagerAutoConfiguration.class.getName(),
                    // 声明式事务管理
                    TransactionAutoConfiguration.class.getName()
            };
        }
    }
}
